
#import "MyInformController.h"
#import "PersonNavView.h"
#import "MyInformCell.h"
#import "UIImageView+WebCache.h"
#import "GTMBase64.h"
#import "MyInformTabHead.h"
#import "MyinformFoot.h"
#import "MyInformChangeController.h"
#import "PhChangeController.h"
#import "MyNameChangeCtrl.h"

#define ORIGINAL_MAX_WIDTH 100.0f
@interface MyInformController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableiew;
@property (nonatomic,strong) NSString *sex;
@property (nonatomic,strong) NSData *imagdata;
@property (nonatomic,strong) UITextView *label;
@property (nonatomic,strong) NSArray *tabAry;
@property (nonatomic,strong) MyInformTabHead *tabHead;
@property (nonatomic,strong) MyinformFoot *tabFoot;
@end

@implementation MyInformController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    StatusBlack;
}
- (IBAction)GoBackToo:(id)sender {
    
     _tabFoot.fatherCtrl = nil;
    _tabHead.fatherctrl = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    _tabAry = @[@"昵称",@"性别",@"手机号",@"QQ"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableiew = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, screen_height) style:UITableViewStyleGrouped];
    _tableiew.dataSource= self;
    _tableiew.delegate = self;
 
    [_tableiew registerClass:[MyInformCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableiew];
    _tableiew.tableHeaderView = self.tabHead;
    _tableiew.tableFooterView = self.tabFoot;
}

-(MyInformTabHead *)tabHead{
    if (!_tabHead) {
        _tabHead = [[NSBundle mainBundle]loadNibNamed:@"MyInformTabHead" owner:nil options:nil].lastObject;
        _tabHead.frame  = CGRectMake(0, 0, SCREEN_WIDTH, screen_height*0.15);
        _tabHead.userHeadUrl = self.headUrl;
        _tabHead.fatherctrl = self;
    }
    return _tabHead;
}

-(void)setModel:(UserDataModel *)model{
    _model = model;
       [self.tabHead.userHeader sd_setImageWithURL:[NSURL URLWithString:_model.avatar] placeholderImage:[UIImage imageNamed:@"the_default_avatar"]];
}

-(MyinformFoot *)tabFoot{
    if (!_tabFoot) {
        _tabFoot = [[NSBundle mainBundle]loadNibNamed:@"MyinformFoot" owner:nil options:nil].lastObject;
        _tabFoot.frame  = CGRectMake(0, 0, SCREEN_WIDTH, 60);
        _tabFoot.fatherCtrl = self;
    }
    return _tabFoot;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInformCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = 0;
    cell.label.text = _tabAry[indexPath.row];
    cell.detailLabel.textColor = [UIColor lightGrayColor];
    
    switch (indexPath.row) {
        case 0:
            
            if (self.model.username) {
                cell.detailLabel.text = self.model.username;
              
            }else{
                cell.detailLabel.text = @"昵称";
            }
        break;
            
            
        case 1:
            
            if (!self.model.sex) {
                cell.detailLabel.text = @"请选择性别";
            }else{
      
                cell.detailLabel.text = self.model.sex;
            }
        break;
            
            
        case 2:
            if (self.model.mobile.length!=0) {
                cell.detailLabel.text = self.model.mobile;
               
            }else{
                cell.detailLabel.text = @"绑定手机号";
            }
        break;
            
        case 3:
            
    
            if (self.model.qq.length!=0) {
                cell.detailLabel.text = self.model.qq;
                
            }else{
                cell.detailLabel.text = @"绑定QQ号";
            }
            
        break;
            
        default:
            break;
    }
    

      return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MyInformCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    __weak typeof(self) weakSelf = self;
    
    if (indexPath.row==1) {
        [self AlerWhithStyleWhithIndexPath:indexPath];
    }else if (indexPath.row==3){
        MyInformChangeController *change = [MyInformChangeController new];
        if (!([cell.detailLabel.text rangeOfString:@"绑定"].length!=0)) {
            change.qqNumber = cell.detailLabel.text;
        }
        change.QqChangeBlock = ^(NSString *qq){
            cell.detailLabel.text = qq;
            [weakSelf ChangeInformWhithChoice:@"qq" AndDic:@{@"qq":qq}];
        };
        
        [self.navigationController pushViewController:change animated:YES];
    
    }else if (indexPath.row==2){//修改手机号码
        PhChangeController *change = [PhChangeController new];
        change.ChangeBlock = ^(NSString *phNumber){
        
            cell.detailLabel.text = phNumber;
            
        };
        [self.navigationController pushViewController:change animated:YES];
        
    }else if (indexPath.row==0){//修改昵称
        
        MyNameChangeCtrl *nameChange = [MyNameChangeCtrl new];
         nameChange.text = cell.detailLabel.text;
        
        
        nameChange.ChangeBlock = ^(NSString *changeName){
            
            cell.detailLabel.text = changeName;
            [weakSelf.app GetUserInform];
        };
        [self.navigationController pushViewController:nameChange animated:YES];
       
    }

}


-(void)AlerWhithStyleWhithIndexPath:(NSIndexPath *)indexPath{
    
    MyInformCell *cell = [self.tableiew cellForRowAtIndexPath:indexPath];
    
    
    UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *Top = [UIAlertAction actionWithTitle:@"保密" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        cell.detailLabel.text = @"保密";
        
        [self ChangeInformWhithChoice:@"sex" AndDic:@{@"sex":@0}];
        
    }];
    

    
    UIAlertAction *men = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        cell.detailLabel.text = @"男";
        
        [self ChangeInformWhithChoice:@"sex" AndDic:@{@"sex":@1}];
        
    }];
    
    UIAlertAction *women = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
         cell.detailLabel.text = @"女";
        [self ChangeInformWhithChoice:@"sex" AndDic:@{@"sex":@2}];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [aler addAction:Top];
    [aler addAction:men];
    [aler addAction:women];
    [aler addAction:cancel];
    
    [self presentViewController:aler animated:YES completion:nil];
}

-(void)ChangeInformWhithChoice:(NSString *)choice AndDic:(NSDictionary *)ChangeDic{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic addEntriesFromDictionary:@{
                                   
                                   @"version":@"4",
                                   @"module":@"member",
                                   @"action":@"modify_info",
                                   @"choice":choice,

                                   }];

    [dic addEntriesFromDictionary:ChangeDic];
    
    [[Manager sharedManager]ResponseWhithURL:@"http://api.diaoyur.cn/app/index.php" AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            [self.app GetUserInform];

        }
        
    }];
}

-(void)viewWillDisappear:(BOOL)animated{

   
}

@end